﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisNet.DataMapper;

namespace StateGrid.OperationLog
{
    /// <summary>
    /// 日志表的数据库操作
    /// </summary>
    public class OperationLog_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();
        /// <summary>
        /// 插入日志项。
        /// </summary>
        /// <param name="logItem"></param>
        public void insertLogItem(OperationLogItem logItem)
        {
            try
            {
                sqlMapper.Insert("InsertOperationLogItem", logItem);
            }
            catch (Exception e)
            {
                Console.WriteLine("插入日志失败：" + e.Message);
            }
        }

        /// <summary>
        /// 按ID号查找日志的详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationLogItem SelectLogByID(int id)
        {
            return sqlMapper.QueryForObject<OperationLogItem>("SelectLogByID", id);
        }

        /// <summary>
        /// 根据条件进行日志查询，返回数据库中的日志记录。
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public IList<OperationLogItem> queryLogItem(LogQueryCondition cond)
        {
            return sqlMapper.QueryForList<OperationLogItem>("QueryOperationLogItem", cond);
        }
        /// <summary>
        /// 查询指定条件，日志的记录数
        /// </summary>
        /// <param name="cond">条件</param>
        /// <returns></returns>
        public int CountOperationLogItem(LogQueryCondition cond)
        {
            return sqlMapper.QueryForObject<int>("CountOperationLogItem", cond);
        }
        
        /// <summary>
        /// 获取操作的本数。
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public int CountBook(LogQueryCondition cond)
        {
            return sqlMapper.QueryForObject<int>("CountBook", cond);
        }
        /// <summary>
        /// 获取操作次数。
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public int CountOperation(LogQueryCondition cond)
        {
            return sqlMapper.QueryForObject<int>("CountOperation", cond);
        }
        /// <summary>
        /// 获取操作涉及的全文总页数。
        /// </summary>
        /// <param name="cond"></param>
        /// <returns></returns>
        public Int64 CountPageCount(LogQueryCondition cond)
        {
            Int64 result=0;
            try
            {
                result = sqlMapper.QueryForObject<Int64>("CountFullTextPage", cond);
            }
            catch (Exception)
            {
            }
            return result;
        }

        /// <summary>
        /// 删除某个日期之前的所有日志。
        /// </summary>
        /// <param name="endDate">设定的日期。不能为null。</param>
        public void deleteLogItemBefore(DateTime endDate)
        {
            sqlMapper.Delete("DeleteOperationLogItemBefore", endDate);
        }

    }
}
